//
//  CommentHeaderView.m
//  HaHa
//
//  Created by 元虎的mac on 15/9/23.
//  Copyright (c) 2015年 吉亚平. All rights reserved.
//

#import "CommentHeaderView.h"

@implementation CommentHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubViews];
//        self.backgroundColor = [UIColor grayColor];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(redyPaly) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopVideoPaly) name:@"back" object:nil];
    return self;
}
//添加控件
- (void)addSubViews{
    
    self.authorPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 62, 62)];
    self.authorPhoto.layer.masksToBounds = YES;
    self.authorPhoto.layer.borderColor = [[UIColor brownColor] CGColor];
    self.authorPhoto.layer.borderWidth = 1;
    self.authorPhoto.layer.cornerRadius = self.authorPhoto.width/2;
    self.authorPhoto.backgroundColor = KArcColor;
    [self addSubview:self.authorPhoto];
    
    self.TitleLabel = [[FlowSubtitles alloc] initWithFrame:CGRectMake(_authorPhoto.X_width+5, _authorPhoto.Y_height- 30, self.width-_authorPhoto.X_width-20, 30) str:@"" Font:17];
    [self addSubview:self.TitleLabel];
    
    self.DownloadButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.DownloadButton.frame  = CGRectMake(30,self.height-32.5, 30, 30);
    [self.DownloadButton setImage:[UIImage imageNamed:@"download"] forState:(UIControlStateNormal)];
    self.DownloadButton.layer.cornerRadius = 8;
    [self addSubview:self.DownloadButton];
    
    self.shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.shareButton.frame = CGRectMake(self.width - 80, self.height- 32.5, 30, 30);
    self.shareButton.layer.cornerRadius = 8;
    [self.shareButton setImage:[UIImage imageNamed:@"share"] forState:(UIControlStateNormal)];
    [self addSubview:self.shareButton];
    
    self.authorName = [[UILabel alloc] initWithFrame:CGRectMake(_TitleLabel.X, 5, self.width-_authorPhoto.X_width-5-95-5, 30)];
    self.authorName.textColor = KgrayColor;
    self.authorName.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.authorName];
    
    self.playCount = [[UILabel alloc] initWithFrame:CGRectMake(self.width- 95, _authorName.Y, 80, 15)];
    self.playCount.textColor = KgrayColor;
    self.playCount.font = [UIFont boldSystemFontOfSize:11];
    self.playCount.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.playCount];
    
    self.createTime = [[UILabel alloc] initWithFrame:CGRectMake(_playCount.X, _playCount.Y_height+1, _playCount.width, 15)];
    self.createTime.textColor = KgrayColor;
    self.createTime.font = [UIFont boldSystemFontOfSize:11];
    self.createTime.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.createTime];
    
    self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.activity.frame = CGRectMake(100, 100, 100, 100);
    
    
    UIImageView *praise = [[UIImageView alloc] initWithFrame:CGRectMake(self.width/2-70, _DownloadButton.Y, 30, 30)];
    praise.image = [UIImage imageNamed:@"praise"];
    [self addSubview:praise];

    
    self.praiseCount = [[UILabel alloc] initWithFrame:CGRectMake(praise.X_width-5, praise.Y+3, 40, 30)];
    self.praiseCount.font = [UIFont systemFontOfSize:11];
    self.praiseCount.textColor = KgrayColor;
    [self addSubview:self.praiseCount];
    
    UIImageView *buried = [[UIImageView alloc] initWithFrame:CGRectMake(self.width/2+10, _DownloadButton.Y, 30, 30)];
    buried.image = [UIImage imageNamed:@"buried"];
    [self addSubview:buried];
    
    self.buried = [[UILabel alloc] initWithFrame:CGRectMake(buried.X_width-5, buried.Y+3, 40, 30)];
    self.buried.font = [UIFont systemFontOfSize:11];
    self.buried.textColor = KgrayColor;
    [self addSubview:self.buried];

    
}
//播放改网址视频
- (void)PlayWithUrlStr:(NSString *)UrlStr{
        [self.activity startAnimating];
    NSURL *URL = [NSURL URLWithString:UrlStr];
    if (!_MediaPlay) {
        _MediaPlay = [[MPMoviePlayerController alloc] initWithContentURL:URL];
    }
    self.MediaPlay.view.frame = CGRectMake(0, 70, self.width, self.height-100);
    _MediaPlay.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|MPMovieScalingModeAspectFill;
    _MediaPlay.repeatMode = MPMovieRepeatModeOne;
    [self addSubview:_MediaPlay.view];
    
    self.activity.frame = self.MediaPlay.view.bounds;
    [self.MediaPlay.view addSubview:self.activity];

    [_MediaPlay play];
}



- (void)SetContentWithMode:(HotVideoMode *)Mode{
    [self.authorPhoto sd_setImageWithURL:[NSURL URLWithString:[Mode.user objectForKey:@"avatar_url"]] placeholderImage:nil];
    self.authorName.text = [Mode.user objectForKey:@"name"];
    [self.TitleLabel SetTitleText:Mode.text WithFont:17];
    self.playCount.text = [NSString stringWithFormat:@"%ld次播放",Mode.play_count];
    self.createTime.text = Mode.cretatime;
    self.buried.text = [NSString stringWithFormat:@"%ld",Mode.bury_count];
    self.praiseCount.text = [NSString stringWithFormat:@"%ld",Mode.favorite_count];
}

//收到通知后停止风火轮
- (void)redyPaly{
    [self.activity stopAnimating];
}

//收到返回通知后停止播放
- (void)stopVideoPaly{
    [self.MediaPlay stop];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
